<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package  	PyroCMS
 * @subpackage  Categories
 * @category  	Module
 * @author  	PyroCMS Dev Team
 */
class Admin_Service extends Admin_Controller {

	/**
	 * The current active section
	 * @access protected
	 * @var int
	 */
	protected $section = 'escort-service';
	protected $_ci;
	/**
	 * The constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->_ci = &get_instance();
		$this->lang->load('be_escort');
		$this->lang->load('be_escort_service');
	}
	
	/**
	 * Index method, lists all categories
	 * @access public
	 * @return void
	 */
	public function index($id)
	{
		$id OR redirect('admin/be_escort');
		
		$escort = $this->_ci->escort_entity->init('id', $id);
		$user = $this->_ci->user_entity->init('id', $escort->getUserId());
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $id);
		if(!$lockedEscort){
			$this->_ci->locked_escort_entity->createNewLockedEscortEntity($id);
			$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $id);
		}
		
		if(!$escort || !$user){
			die();
		}
		$this->data['escort'] = $escort;
		$this->data['user'] = $user;
		$this->data['lockedEscort'] = $lockedEscort;
		$this->pyrocache->delete_all('modules_m');
		$this->load->view('admin/service/index', $this->data);
	}
	public function save(){
		$result = array();
		$result['result'] = true;
		$escortId = $this->input->post('escort_id');
		$weekday_array = array_filter(explode(',', isset($_POST['services_weekdays']) ? implode(',', $_POST['services_weekdays']):'' ) );
		$ESCORT = $this->escort_entity->init('id',$escortId);
		$USER = $this->_ci->user_entity->init('id', $ESCORT->getUserId());
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $escortId);
		$weekdaySelected = $ESCORT->getWeekdaysArray($USER->getId());
		foreach($weekdaySelected as $id){
			if(! in_array($id,$weekday_array)){
				$this->db->where(array('user_id'=>$USER->getId(), 'week_day'=>$id))->delete(TBL_SCHEDULE);
			}
		}
		foreach($weekday_array as $id){
			$from = $this->input->post("services_time_from_$id",TRUE);
			$to = $this->input->post("services_time_to_$id",TRUE);
			$ESCORT->setSchedule($ESCORT->getUserId(), $id, $from, $to);
		}
		
		$services_public_schedule = $this->input->post('public_schedule', TRUE);
		$services_public_calendar = $this->input->post('public_working_calendar', TRUE);
		$services_public_working_places = $this->input->post('public_working_places', TRUE);
		$services_public_travels = $this->input->post('public_travel',TRUE);
		$services_public_serves = $this->input->post('public_serves', TRUE);
		$services_public_services = $this->input->post('public_services', TRUE);
		
		$services_working_places_ids = isset($_POST['services_working_places']) ? implode(',', $_POST['services_working_places']):'';
		$services_travels_ids = isset($_POST['services_travels']) ? implode(',', $_POST['services_travels']):'';
		$services_serves_ids = isset($_POST['services_serves']) ? implode(',', $_POST['services_serves']):'';
		$services_services_ids = isset($_POST['services_services']) ? implode(',', $_POST['services_services']):'';
		
		$ESCORT->setPublicSchedule($services_public_schedule);
		$ESCORT->setPublicWorkingCalendar($services_public_calendar);
		$ESCORT->setPublicWorkingPlaces($services_public_working_places);
		$ESCORT->setPublicTravel($services_public_travels);
		$ESCORT->setPublicServesTo($services_public_serves);
		$ESCORT->setPublicServices($services_public_services);
		$ESCORT->setWorkingPlaceIds($services_working_places_ids);
		$ESCORT->setTravelIds($services_travels_ids);
		$ESCORT->setServesToIds($services_serves_ids);
		$ESCORT->setServiceIds($services_services_ids);
		$ESCORT->adminUpdate();		
		
		//$lockedEscort = new CI_Locked_escort_entity();
		$lockedEscort->setLockedSchedule($this->input->post('locked_schedule'));
		$lockedEscort->setLockedWorkingCalendar($this->input->post('locked_working_calendar'));
		$lockedEscort->setLockedWorkingPlaces($this->input->post('locked_working_places'));
		$lockedEscort->setLockedTravel($this->input->post('locked_travel'));
		$lockedEscort->setLockedServeTo($this->input->post('locked_serves'));
		$lockedEscort->setLockedServices($this->input->post('locked_services'));
															 
		$lockedEscort->update();
		$result['lockedEscort'] = $lockedEscort->getEscortId();
		$result['message'] = lang('escort_be_escort_service_update_success');
		echo json_encode($result);
	}
	function loadAsyncDaysInMonth(){
		$id = $this->input->post('escort_id');
		$year = $this->input->post('year',TRUE);
		$month = $this->input->post('month',TRUE);
		$escort = $this->_ci->escort_entity->init('id', $id);
		$user = $this->_ci->user_entity->init('id', $escort->getUserId());
				
		$WORKING_OBJ = $escort->getWorkingCalendar($user->getId(), $year, $month);
		$dateArray = $WORKING_OBJ ? explode(',',$WORKING_OBJ->dates) : array() ;
		echo multi_checkbox($name_array = "days_in_month[]", $value_key_array= daysInMonthDataArray( $month, $year ), $value_checked=$dateArray, $divStyle='', $inputStyle ='', true);
	}
}